<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use think\Db;
use think\Request;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\validate\UserValidate;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class FaorderController extends Controller
{

    /**
     * 油票充值订单
     * User:@hang
     */
    public function stamporder(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        if (isset($param['type']) && $param['type'] != '') {
            $whehe[] = ['a.type','=',$param['type']];
        }
        if (isset($param['qd']) && $param['qd'] != '') {
            $whehe[] = ['b.me_qd','=',$param['qd']];
        }
        if (isset($param['phone_agent']) && $param['phone_agent'] != '') {
            $whehe[] = ['a.phone_agent','=',$param['phone_agent']];
        }
        if (isset($param['order_number']) && $param['order_number'] != '') {
            $whehe[] = ['a.order_number','like',"%{$param['order_number']}%"];
        }
        if (isset($param['fmember_id']) && !empty($param['fmember_id'])) {
            $whehe[] = ['b.me_parent','=',$param['fmember_id']];
        }

        $whehetime = '';
        $dr_time_date = '';
        if (isset($param['add_time'])) {
            if(!empty($param['add_time'])){
                $time_arr = explode(' - ', $param['add_time']);
                $start_time = strtotime($time_arr[0]);
                $end_time = strtotime($time_arr[1]);
                $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
            }
        }else{
            //当日时间
            $dr_start_time = strtotime(date('Y-m-d').'00:00:00');//今日凌晨时间戳
            $dr_end_time = $dr_start_time + 86399;
            $dr_time_date = date('Y-m-d H:i:s',$dr_start_time).' - '.date('Y-m-d H:i:s',$dr_end_time);
            $whehetime = "a.add_time >= $dr_start_time AND a.add_time <= $dr_end_time";
        }

        $whehetimezc = '';
        if (isset($param['me_add_time']) && !empty($param['me_add_time'])) {
            $time_arr = explode(' - ', $param['me_add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1]);
            $whehetimezc = "b.me_add_time >= $start_time AND b.me_add_time <= $end_time";
        }

        $IndexTableModel = New IndexTableModel();
        $data = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_member f",'a.fid = f.member_id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->where($whehetimezc)
            ->field('a.*,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,b.me_bind_type,b.me_bind_admin,f.member_id as f_member_id,f.me_uid as f_me_uid,f.me_phone as f_me_phone,f.me_sex as f_me_sex,f.me_nick_name as f_me_nick_name,f.me_status as f_me_status,b.me_qd')
            ->order('a.id desc,a.add_time desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $adminid = $this->uid;
        $data->each(function ($v, $k)use($IndexTableModel,$adminid) {
            //只有超管能看全部手机号
            if($adminid != 1){
                $v['me_phone'] = hide_phone_number($v['me_phone']);
                $v['f_me_phone'] = hide_phone_number($v['f_me_phone']);
            }
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            switch($v['type']){
                case '1': $v['type_word'] = '微信';break;
                case '2': $v['type_word'] = '支付宝';break;
            }
            switch($v['phone_agent']){
                case 'android': $v['phone_agent_word'] = '安卓';break;
                case 'ios': $v['phone_agent_word'] = 'IOS';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);

            if(!empty($v['f_member_id'])){
                switch($v['f_me_sex']){
                    case '0': $v['f_me_sex_word'] = '未设置';break;
                    case '1': $v['f_me_sex_word'] = '男';break;
                    case '2': $v['f_me_sex_word'] = '女';break;
                }
                if(!empty(strpos($v['f_me_phone'], 'zx'))){
                    $v['f_me_phone'] = explode('zx',$v['f_me_phone'])[0].'（已注销）';
                    $v['f_me_nick_name'] = explode('zx',$v['f_me_nick_name'])[0].'（已注销）';
                }
            }

            $v['me_bind_type_word'] = '-';
            switch ($v['me_bind_admin']) {
                case 1:$v['me_bind_type_word'] = '扫码绑定';break;
                case 2:$v['me_bind_type_word'] = '注册绑定';break;
                case 3:$v['me_bind_type_word'] = 'VIP绑定';break;
                case 4:$v['me_bind_type_word'] = '漏绑绑定';break;
                case 5:$v['me_bind_type_word'] = '后台绑定';break;
            }

            $djMemberId = getDjParent($v['member_id']);
            $v['dj_member_id'] = $djMemberId['dj_member_id'];
            if(empty($djMemberId['bz_name'])){
                $v['dj_member_word'] = empty($djMemberId['dj_member_id'])?'自身':$djMemberId['dj_member_id'];
            }else{
                $v['dj_member_word'] = empty($djMemberId['dj_member_id'])?'自身':$djMemberId['dj_member_id'].'【'.$djMemberId['bz_name'].'】';
            }

            $v['me_qd'] = !empty($v['me_qd'])?$v['me_qd']:'-';
            return $v;
        });

        //微信总充值
        $wxpaymoney = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where("`type` = 1")
            ->where($whehe)
            ->where($whehetime)
            ->where($whehetimezc)
            ->sum('a.pay_money');
        //微信信息流充值
        $wxpaymoneyXxl = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where("`type` = 1 and b.me_qd != ''")
            ->where($whehe)
            ->where($whehetime)
            ->where($whehetimezc)
            ->sum('a.pay_money');
        //支付宝总充值
        $zfbpaymoney = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where("`type` = 2")
            ->where($whehe)
            ->where($whehetime)
            ->where($whehetimezc)
            ->sum('a.pay_money');
        //支付宝信息流充值
        $zfbpaymoneyXxl = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where("`type` = 2 and b.me_qd != ''")
            ->where($whehe)
            ->where($whehetime)
            ->where($whehetimezc)
            ->sum('a.pay_money');


        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            'dr_time_date' => $dr_time_date,
            'wxpaymoney' => $wxpaymoney,//微信总充值
            'zfbpaymoney' => $zfbpaymoney,//支付宝总充值
            'totalpaymoney' => delete0(bcadd($wxpaymoney,$zfbpaymoney,2)),//总充值
            'wxpaymoneyXxl' => $wxpaymoneyXxl,//微信信息流充值
            'zfbpaymoneyXxl' => $zfbpaymoneyXxl,//支付宝信息流充值
            'wxpaymoneyXxlNo' => delete0(bcsub($wxpaymoney,$wxpaymoneyXxl,2)),//微信非信息流充值
            'zfbpaymoneyXxlNo' => delete0(bcsub($zfbpaymoney,$zfbpaymoneyXxl,2)),//支付宝非信息流充值
            'wxXxlZ' => delete0(bcadd($wxpaymoneyXxl,$zfbpaymoneyXxl,2)),//信息流总充值
            'zfbXxlZ' => delete0(bcsub(bcsub(bcadd($wxpaymoney,$zfbpaymoney,2),$wxpaymoneyXxl,2),$zfbpaymoneyXxl,2)),//非信息流总充值
        ]);
        return $this->fetch();
    }


    /**
     * 充值VIP订单
     * User:@hang
     */
    public function viporder(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        if (isset($param['order_number']) && $param['order_number'] != '') {
            $whehe[] = ['a.order_number','like',"%{$param['order_number']}%"];
        }
        $whehetime = '';
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $time_arr = explode(' - ', $param['add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
        }
        $whehe[] = ['a.admin_id','=',0];
        $IndexTableModel = New IndexTableModel();
        $data = Db::table('fa_recharge_vip_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->field('a.*,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status')
            ->order('a.id desc,a.add_time desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $data->each(function ($v, $k)use($IndexTableModel) {
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            return $v;
        });

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }


    /**
     * 充值曝光订单
     * User:@hang
     */
    public function exposureorder(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        if (isset($param['order_number']) && $param['order_number'] != '') {
            $whehe[] = ['a.order_number','like',"%{$param['order_number']}%"];
        }
        $whehetime = '';
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $time_arr = explode(' - ', $param['add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
        }
        $IndexTableModel = New IndexTableModel();
        $data = Db::table('fa_recharge_exposure_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->field('a.*,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status')
            ->order('a.id desc,a.add_time desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $data->each(function ($v, $k)use($IndexTableModel) {
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            return $v;
        });

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }


    /**
     * 破圈层订单
     * User:@hang
     */
    public function circleorder(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        if (isset($param['order_number']) && $param['order_number'] != '') {
            $whehe[] = ['a.order_number','like',"%{$param['order_number']}%"];
        }
        $whehetime = '';
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $time_arr = explode(' - ', $param['add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
        }
        $IndexTableModel = New IndexTableModel();
        $data = Db::table('fa_recharge_circle_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_circle_list c",'a.circle_id = c.id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->field('a.*,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,c.circle_name')
            ->order('a.id desc,a.add_time desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $data->each(function ($v, $k)use($IndexTableModel) {
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            return $v;
        });

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }


    /**
     * 油票充值排行
     * User:@hang
     */
    public function rechargeranking(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        if (isset($param['phone_agent']) && $param['phone_agent'] != '') {
            $whehe[] = ['b.phone_agent','=',$param['phone_agent']];
        }
        if (isset($param['me_bind_type']) && $param['me_bind_type'] != '') {
            $whehe[] = ['b.me_bind_type','=',$param['me_bind_type']];
        }
        $whehetime = '';
        $tjwheretime = '';
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $time_arr = explode(' - ', $param['add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
            $tjwheretime = " and cz.add_time >= $start_time AND cz.add_time <= $end_time";
        }

        $IndexTableModel = New IndexTableModel();
//        //带最后充值时间
//        $data = Db::table('fa_recharge_stamp_order')
//            ->alias('a')
//            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
//            ->rightJoin("(SELECT max(id) as maxid FROM fa_recharge_stamp_order WHERE member_id != 0".$groupwheretime." GROUP BY member_id  ORDER BY maxid DESC) t2","t2.maxid = a.id")
//            ->where($whehe)
//            ->where($whehetime)
//            ->where("b.member_id is not null")
//            ->field("a.id,a.add_time,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,(select sum(cz.pay_money) from fa_recharge_stamp_order cz where cz.member_id = a.member_id".$tjwheretime.") as cz_all_money,(select count(cz.id) from fa_recharge_stamp_order cz where cz.member_id = a.member_id".$tjwheretime.") as cz_all_num")
//            ->order('cz_all_money desc,a.id desc')
//            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $data = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_member f",'b.me_parent = f.member_id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->where("b.member_id is not null")
            ->field("a.id,a.add_time,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,b.phone_agent,b.me_bind_type,f.member_id as f_member_id,f.me_uid as f_me_uid,f.me_phone as f_me_phone,f.me_sex as f_me_sex,f.me_nick_name as f_me_nick_name,f.me_status as f_me_status,(select sum(cz.pay_money) from fa_recharge_stamp_order cz where cz.member_id = a.member_id".$tjwheretime.") as cz_all_money,(select count(cz.id) from fa_recharge_stamp_order cz where cz.member_id = a.member_id".$tjwheretime.") as cz_all_num,(select sum(fl.money) from fa_rebate_log fl where a.member_id = fl.cmember_id and fl.grade_type = 1 and fl.type in(5)) as bdh_jl_sj_money,b.me_qd")
            ->group("a.member_id")
            ->order('cz_all_money desc,a.id desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $adminid = $this->uid;
        $data->each(function ($v, $k)use($IndexTableModel,$adminid) {
            //只有超管能看全部手机号
            if($adminid != 1){
                $v['me_phone'] = hide_phone_number($v['me_phone']);
            }
            switch($v['me_bind_type']){
                case '0': $v['me_bind_type_word'] = '未绑定上级';break;
                case '1': $v['me_bind_type_word'] = '扫码绑定';break;
                case '2': $v['me_bind_type_word'] = '充值绑定';break;
            }
            switch($v['phone_agent']){
                case 'android': $v['phone_agent_word'] = '安卓';break;
                case 'ios': $v['phone_agent_word'] = 'IOS';break;
            }
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
//            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);

            if(!empty($v['f_member_id'])){
                switch($v['f_me_sex']){
                    case '0': $v['f_me_sex_word'] = '未设置';break;
                    case '1': $v['f_me_sex_word'] = '男';break;
                    case '2': $v['f_me_sex_word'] = '女';break;
                }
                if(!empty(strpos($v['f_me_phone'], 'zx'))){
                    $v['f_me_phone'] = explode('zx',$v['f_me_phone'])[0].'（已注销）';
                    $v['f_me_nick_name'] = explode('zx',$v['f_me_nick_name'])[0].'（已注销）';
                }
            }
            $v['bdh_jl_sj_money'] = !empty($v['bdh_jl_sj_money'])?$v['bdh_jl_sj_money']:'0';//绑定后奖励给上级
            $v['bdq_pay_money'] = delete0(bcsub($v['cz_all_money'],$v['bdh_jl_sj_money'],2));//绑定前充值
            $v['me_qd'] = !empty($v['me_qd'])?$v['me_qd']:'-';
            return $v;
        });

        $paymoney = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->sum('a.pay_money');

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            'paymoney' => $paymoney,
        ]);
        return $this->fetch();
    }



    /**
     * 油票提现排行
     * User:@hang
     */
    public function withdrawranking(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        $whehetime = '';
        $tjwheretime = '';
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $time_arr = explode(' - ', $param['add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
            $tjwheretime = " and tx.add_time >= $start_time AND tx.add_time <= $end_time";
        }

        $IndexTableModel = New IndexTableModel();
        $data = Db::table('fa_zwithdrawal_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_member f",'b.me_parent = f.member_id','LEFT')
            ->where("a.status = 1")
            ->where($whehe)
            ->where($whehetime)
            ->where("b.member_id is not null")
            ->field("a.id,a.add_time,b.member_id as tx_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,f.member_id as f_member_id,f.me_uid as f_me_uid,f.me_phone as f_me_phone,f.me_sex as f_me_sex,f.me_nick_name as f_me_nick_name,f.me_status as f_me_status,(select sum(tx.actual_withdrawal) from fa_zwithdrawal_order tx where tx.member_id = a.member_id and tx.status = 1".$tjwheretime.") as tx_all_money,(select count(tx.id) from fa_zwithdrawal_order tx where tx.member_id = a.member_id and tx.status = 1".$tjwheretime.") as tx_all_num")
            ->group("a.member_id")
            ->order('tx_all_money desc,a.id desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $data->each(function ($v, $k)use($IndexTableModel) {
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
//            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);

            if(!empty($v['f_member_id'])){
                switch($v['f_me_sex']){
                    case '0': $v['f_me_sex_word'] = '未设置';break;
                    case '1': $v['f_me_sex_word'] = '男';break;
                    case '2': $v['f_me_sex_word'] = '女';break;
                }
                if(!empty(strpos($v['f_me_phone'], 'zx'))){
                    $v['f_me_phone'] = explode('zx',$v['f_me_phone'])[0].'（已注销）';
                    $v['f_me_nick_name'] = explode('zx',$v['f_me_nick_name'])[0].'（已注销）';
                }
            }
            return $v;
        });

        $txmoney = Db::table('fa_zwithdrawal_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where("a.status = 1")
            ->where($whehe)
            ->where($whehetime)
            ->sum('a.actual_withdrawal');

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            'txmoney' => $txmoney,
        ]);
        return $this->fetch();
    }



    /**
     * 为此用户设置备注姓名 页面
     * User:@hang
     */
    public function setbzname($id)
    {
        //查询提现详情
        $IndexTableModel = New IndexTableModel();
        $memberInfo = $IndexTableModel->getFind('fa_member',"`member_id` = '{$id}'",'member_id,me_uid,me_phone,me_nick_name,me_sex,me_realname,me_realidcard,bz_name');
        $this->assign([
            'data' => $id,
            'memberinfo' => $memberInfo,
        ]);
        return $this->fetch();
    }


    /**
     * 确认 修改用户备注姓名
     * User:@hang
     */
    public function actionsetbzname()
    {
        $id = input('id');
        $bz_name = input('bz_name');
        $IndexTableModel = New IndexTableModel();
        //操作上锁
        $Redis_Lock  =  new \Redislock();
        $otc_sell_key  =  'lock:actionsetbzname'.$id;	// 定义锁标识
        $is_lock  =  $Redis_Lock->lock($otc_sell_key, 15);	// 获取锁
        //解锁失败
        if(!$is_lock){
            return admin_error('操作频繁，请稍后再试',URL_CLOSE_REFRESH);
        }
        $IndexTableModel->editInfo('fa_member',"`member_id` = {$id}",['bz_name'=>$bz_name]);
        $Redis_Lock->unlock($otc_sell_key);	//释放锁
        return admin_success('设置成功',URL_CLOSE_REFRESH);
    }


}
